package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.dongdong.Cart;
import com.dongdong.pojo.TbUser;
import com.dongdong.service.UserService;
import com.dongdongshop.util.CookieUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("cartController")
public class CartController {

    @Reference(timeout = 500000)
    private UserService userService;

    @RequestMapping("shuju")
    @ResponseBody
    public boolean shuju(HttpServletRequest request, HttpServletResponse response,Long itemId,Integer num){
        saveCart(request, response, itemId, num);
        return true;
    }

    @RequestMapping("saveCart")                                 //                       商品  ID      数量
        public String saveCart(HttpServletRequest request, HttpServletResponse response,Long itemId,Integer num){
//        获取登录用户
        TbUser  user = (TbUser) SecurityUtils.getSubject().getPrincipal();
//      用户没有登录的情况添加购物车
//        先从Cookie中查询原来的购物车
        List<Cart> cartList = getCart(request,response);
//         组装购物车
        List<Cart> cartList1 = userService.save(cartList,itemId,num);

        if(user == null){
//        保存数据
            CookieUtils.setCookie(request,response,"cartList",JSONObject.toJSONString(cartList1),true);
        }else {
            userService.saveCartList(cartList1,user.getUsername());
        }
        return "/success-cart";
    }


    @RequestMapping("getCart")
    @ResponseBody
    public List<Cart> getCart(HttpServletRequest request,HttpServletResponse response){
//        获取登录用户
        TbUser  user = (TbUser) SecurityUtils.getSubject().getPrincipal();
        String cartListStr = CookieUtils.getCookieValue(request, "cartList", true);
        if(StringUtils.isBlank(cartListStr)){
            cartListStr = "[]";
        }
//          Cookie中获取购物车         虽然是字符串  但是格式是[]
        List<Cart> cartListCookie = JSONObject.parseArray(cartListStr,Cart.class);
//        未登录
        if(user == null){
//            返回Cookie中购物车
            return cartListCookie;
        }else{
//            从redis中获取购物车
            List<Cart> cartListRedis  = userService.getCartList(user.getUsername());
//            如果Cookie中有数据再返回数据
            if(cartListCookie.size() > 0) {
//            合并购物车
                List<Cart> cartList = userService.merageCartList(cartListRedis, cartListCookie);
//              将合并后的购物车数据重新保存到redis中
                userService.saveCartList(cartList, user.getUsername());
//            删除 cookie中数据
                CookieUtils.deleteCookie(request, response, "cartList");
                return cartList;
            }else {  // 如果没有数据直接返回当亲前缓存中的数据
//            返回的从redis中购物车
                return cartListRedis;
            }
        }

    }
}
